package com.CmJava.controller;

import com.CmJava.entity.ResultEntity;
import com.CmJava.entity.User;
import com.CmJava.entity.UserDetail;
import com.CmJava.service.PrivateService;
import com.CmJava.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController //相当于@ResponseBody和@Controller合在一起的作用
public class UserController {

    @Reference
    private PrivateService privateService;

    @Reference
    private UserService userService;


    // 注册
    @PostMapping("register")
    public ResultEntity register(@RequestParam("userName") String userName,
            @RequestParam("password") String password){
        boolean flag = userService.register(userName, password);
        if (flag){
            return ResultEntity.successWithoutMessage();
        }else {
            return ResultEntity.failureWithoutMessage();
        }
    }


    // 获取当前登录者的信息
    @PostMapping("getUserName")
    public ResultEntity getUserName(Authentication authentication){
        return ResultEntity.successWithMessage(authentication.getName());
    }

    // 获取当前登录者的好友列表
    @PostMapping("getFriendList")
    public ResultEntity getFriendList(@RequestParam("userName") String userName){
        String friendList = privateService.getFriendList(userName);
        String friendList0="";
        if (friendList.equals("--")){
            return ResultEntity.successWithMessage(friendList);
        }else {
            String[] userIds = friendList.split(",");
            for (String userId : userIds) {
                User user = userService.getUserById(userId);
                friendList0+=user.getUserName()+",";
            }
            return ResultEntity.successWithMessage(friendList0.substring(0,friendList0.length()-1));
        }
    }

    // 获取当前登录者的每个好友的详细信息
    @PostMapping("getFriendDetail")
    public ResultEntity getFriendDetail(@RequestParam("userName") String userName,@RequestParam("friendList") String friendList){
        List<UserDetail> friendDetail = privateService.getFriendDetail(userName, friendList);
        if (friendDetail!=null && friendDetail.size()>0){
            return ResultEntity.successWithMessage(friendDetail);
        }else {
            return ResultEntity.failureWithoutMessage();
        }
    }


    //找好友
    @PostMapping("findUser")
    public ResultEntity findUser(@RequestParam("userName") String userName) {
        ArrayList<String> strings = new ArrayList<>();
        List<User> userBlur = userService.getUserBlur(userName);
        for (User user : userBlur) {
            strings.add(user.getUserName());
        }
        if (userBlur!=null && userBlur.size()>0){
            return ResultEntity.successWithMessage(strings);
        }
        return ResultEntity.failureWithoutMessage();
    }


    //加好友
    @PostMapping("addFriend")
    public ResultEntity addFriend(@RequestParam("userName") String userName,@RequestParam("targetName") String targetName){
        boolean flag = userService.addFriend(userName, targetName);


        if (flag){
            return ResultEntity.successWithoutMessage();
        }else {
            return ResultEntity.failureWithoutMessage();
        }
    }

    //删好友
    @PostMapping("deleteFriend")
    public ResultEntity deleteFriend(@RequestParam("userName") String userName,@RequestParam("targetName") String targetName){
        boolean flag = userService.deleteFriend(userName, targetName);
        if (flag){
            return ResultEntity.successWithoutMessage();
        }else {
            return ResultEntity.failureWithoutMessage();
        }
    }

}
